

//
//  YDMessageCell.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/1/18.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDMessageCell.h"

@interface YDMessageCell()
/**
 白色背景view
 */
@property (nonatomic, strong)UIView *bgView;


/**
 红点view
 */
@property (nonatomic, strong)UIView *redPoint;

/**
 标题
 */
@property (nonatomic, strong)UILabel *titleLbl;

/**
 日期
 */
@property (nonatomic, strong)UILabel *dateLbl;

/**
 内容
 */
@property (nonatomic, strong)UIWebView *textLbl;


/**
 站内消息的lbel
 */
@property (nonatomic, strong)UILabel *contentLbl;

@end

@implementation YDMessageCell


- (void)setModel:(YDNotiModel *)model
{
    _model = model;
    
    CGFloat fontsize = LKScreenW *font14ScreenWRate;
    
    if (model.isOutside) {
        NSString *chakanHtml = @"，点击 <span class = \"kan\">查看详情 > </span>";
        
        NSString *color = nil;
    
        if ([model.outsideMsgType isEqualToString:@"ACCOUNT_RECHARGESUCCESS"]) {
            color =  [@"#"stringByAppendingString:stockRed];
        }else if ([model.outsideMsgType isEqualToString:@"ACCOUNT_WITHDRAWALSSUCCESS"]||
                  [model.outsideMsgType isEqualToString:@"ACCOUNT_WITHDRAWALFAILED"])
        {
            color = [@"#" stringByAppendingString: stockGreen];
        }else
        {
            color = @"black";
        }
        
        
        
        NSString * htmls = [NSString stringWithFormat:@"<html> \n"
                            "<head> \n"
                            "<style type=\"text/css\"> \n"
                            "body {font-size:%lfpx; color:#687284;text-align: justify;}\n"
                            "span {font-size: %lfpx;color: %@;}"
                            ".kan {color:#3e59a7}"
                            "</style> \n"
                            "</head> \n"
                            "<body>"
                            "%@%@"
                            "</body>"
                            "</html>",fontsize,fontsize,color,model.content,chakanHtml];
        
        [self.textLbl loadHTMLString:htmls baseURL:nil];
       
    }
    else
    {
        NSString * htmls = [NSString stringWithFormat:@"<html> \n"
                            "<head> \n"
                            "<style type=\"text/css\"> \n"
                            "body {font-size:%lfpx; color:#687284;text-align: justify;}\n"
                            "</style> \n"
                            "</head> \n"
                            "<body>"
                            "%@"
                            "</body>"
                            "</html>",fontsize,model.content];
          [self.textLbl loadHTMLString:htmls baseURL:nil];
    }
    
    self.titleLbl.text = model.title;
    if (model.createTime.length) {
        self.dateLbl.text = [model.createTime substringWithRange:NSMakeRange(0, model.createTime.length - 3)];
    }
    
    self.redPoint.hidden = [model.hasRead boolValue];
    
}

+(CGFloat)heightForCellWithModel:(YDNotiModel *)model
{
    NSString *text = [model.content stringByAppendingString:@"，点击查看详情 >"];
    
    CGFloat textH = [NSString getHeightWithText:text font:[UIFont systemFontOfSize:font14ScreenWRate *LKScreenW] MaxWidth:LKScreenW - 2*rate375(30) lineSpace:1.5];

    return textH + (isPhoneX ? rateX812(60) :rate750(60));
}

+ (instancetype)cellWithTbaleview:(UITableView *)tableview
{
    static NSString *const PZMessageCellID = @"PZMessageCellID";
    
    YDMessageCell *cell = [tableview dequeueReusableCellWithIdentifier:PZMessageCellID];
    if (cell == nil) {
        cell = [[YDMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PZMessageCellID];
        cell.backgroundColor = [UIColor colorWithHexString:mainGray];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self bgView];
        [self titleLbl];
        [self redPoint];
        [self dateLbl];
        [self textLbl];
    
    }
    return self;
}


-(UIView *)bgView
{
    if (_bgView == nil) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 4;
        _bgView.layer.masksToBounds = YES;
        
        [self.contentView addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(rate375(10)));
            make.right.equalTo(@(-rate375(10)));
            if (isPhoneX) {
                make.top.equalTo(@(rateX812(10)));
               // make.height.equalTo(@(rateX812(98)));
            }else
            {
                make.top.equalTo(@(rate750(10)));
               // make.height.equalTo(@(rate750(98)));
            }
            make.height.equalTo(self.contentView.mas_height).multipliedBy(0.9);
            
        }];
    }
    return _bgView;
}

-(UILabel *)titleLbl
{
    if (_titleLbl == nil) {
        _titleLbl = [UILabel new];
        _titleLbl.text = @"点买通知";
        _titleLbl.font = [UIFont systemFontOfSize:font15ScreenWRate *LKScreenW];
        [self.contentView addSubview:_titleLbl];
        [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(rate375(20)));
            make.top.equalTo(_bgView.mas_top).offset(isPhoneX? rateX812(15) : rate750(15));
        }];
    }
    return _titleLbl;
}

-(UIView *)redPoint
{
    if (_redPoint == nil) {
        _redPoint = [UIView new];
        _redPoint.backgroundColor = [UIColor redColor];
        _redPoint.layer.cornerRadius = 4;
        _redPoint.layer.masksToBounds = YES;
        [self.contentView addSubview:_redPoint];
        [_redPoint mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_titleLbl.mas_left);
            make.top.equalTo(_titleLbl.mas_top);
            make.width.height.equalTo(@8);
        }];
        
    }
    return _redPoint;
}

-(UILabel *)dateLbl
{
    if (_dateLbl == nil) {
        _dateLbl = [UILabel new];
        _dateLbl.text = @"2017.12.27  14:11";
        _dateLbl.textAlignment = NSTextAlignmentRight;
        _dateLbl.font = [UIFont systemFontOfSize:font12ScreenWRate *LKScreenW];
        [self.contentView addSubview:_dateLbl];
        [_dateLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_bgView).offset(-rate375(10));
            make.centerY.equalTo(_titleLbl.mas_centerY);
        }];
    }
    return _dateLbl;
}

-(UIWebView *)textLbl
{
    if (_textLbl == nil) {
        _textLbl = [UIWebView new];
        //_textLbl.textColor = [UIColor colorWithHexString:battleshipGrey];
        //_textLbl.lineBreakMode = NSLineBreakByCharWrapping;
        //_textLbl.text = @"您所购买的“中国平安000001”已经入“持仓中”状态，点击 查看详情>";
        //_textLbl.numberOfLines = 0;
        _textLbl.scrollView.scrollEnabled = NO;
        _textLbl.userInteractionEnabled = NO;
        _textLbl.backgroundColor = [UIColor clearColor];
        _textLbl.opaque = NO;
    
        [self.contentView addSubview:_textLbl];
        [_textLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLbl.mas_left).offset(-5);
            make.right.equalTo(_dateLbl.mas_right);
            make.top.equalTo(_titleLbl.mas_bottom);
           // make.height.equalTo(isPhoneX ? @(rateX812(50)) : @(rate750(50)));
            make.height.equalTo(_bgView.mas_height).multipliedBy(0.52);
        }];
    }
    return _textLbl;
}
@end
